Option Strict On

Imports System.Web
Imports System.Xml
Imports System.Collections.Generic

Imports System.Security.Cryptography
Imports System.Web.Configuration

Imports DnnPerm = DotNetNuke.Security.Permissions
Imports DnnRoles = DotNetNuke.Security.Roles
Imports DnnFS = DotNetNuke.Services.FileSystem

Imports System.ServiceModel
Imports System.Runtime.Serialization




Namespace DotNetNuke.Modules.IWebCF

    Partial Public Class CoreService
        Implements IWebFileTransferManager




#Region "IWebFileTransferManager"

        <IWebMethod(Application:="IWeb", Name:="DownloadFile", DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host, _
          Company:="DotNetNuke", Description:="MTOM operation to download a file.", Group1:="IWeb FileManager", Group2:="Portal")> _
       Public Function DownloadFile(ByVal msg As FileIdRequest) As IWebResponse(Of Byte()) Implements IWebFileTransferManager.DownloadFile

            Dim authendication As New IWebAuthentication(msg.Header)

            If authendication.ValidAndAuthorized() _
                AndAlso Me.HasFileAccess(msg.Header, msg.FileId, Permission_ReadId) Then

                Dim fileLocInfo As DotNetNuke.Services.FileSystem.FileInfo

                Dim fileCtrl As New Services.FileSystem.FileController()

                Dim info As FileInfoDataContract = Me.GetFileById(New FileIdRequest(msg.Header, msg.FileId)).Value

                fileLocInfo = fileCtrl.GetFile(IO.Path.GetFileName(info.FileName), _
                    msg.Header.PortalID, info.FolderId)

                Return New IWebResponse(Of Byte())(FileSystemUtils.GetFileContent(fileLocInfo))

            Else


                Throw New FaultException(Of String)("Not Authorized")

            End If
        End Function


        <IWebMethod(Application:="IWeb", Name:="UploadFile", DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host, _
           Company:="DotNetNuke", Description:="MTOM operation to upload a file.", Group1:="IWeb FileManager", Group2:="Portal")> _
       Public Sub UploadFile(ByVal msg As UploadFileRequest) Implements IWebFileTransferManager.UploadFile

            Dim objIWebAuthendication As New IWebAuthentication(msg.Header)
            If Not objIWebAuthendication.ValidAndAuthorized() Then

                Throw New FaultException(Of String)("Not Authorized")

            End If


            If Not Me.HasFolderAccess(msg.Header, msg.FolderId, Permission_WriteId) Then

                Dim soapEx As FaultException = New FaultException _
                    ("Portal disk space quota has been reached, upload has been cancelled. " & _
                    Me.GetPortalSpaceUsed(New IWebRequest(Of Boolean)(msg.Header, True)).Value)

                Exceptions.LogException(soapEx)

                Throw soapEx

            End If

            Dim portalCtrl As New PortalController

            Dim spaceUsed As Long = portalCtrl.GetPortalSpaceUsedBytes(msg.Header.PortalID)

            Dim hostingSpace As Long = CLng(GetPortalInfo(msg.Header.PortalID).HostSpace)

            If hostingSpace = 0 OrElse ConvertToMB(spaceUsed) < hostingSpace Then
                'this will currently allow the start of an upload without knowing if there
                'is space available. must check on every read.

                Try

                    Dim destFilePath As String

                    destFilePath = Me.GetFileMapPath(msg.Header, msg.FolderId, msg.FileName)

                    If Not CheckFileType(IO.Path.GetExtension(msg.FileName)) Then

                        Throw New NotSupportedException("This file type is not supported.")

                    End If

                    If Not (ConvertToMB(spaceUsed) < hostingSpace OrElse hostingSpace = 0) Then

                        'Out of hosting space
                        Throw New InvalidOperationException("Not enough hosting space.")


                    End If

                    Dim drive As New IO.DriveInfo(IO.Path.GetPathRoot(IO.Directory.GetCurrentDirectory).Replace(":\", ""))


                    If drive IsNot Nothing AndAlso (spaceUsed + (500 * 1024) > drive.AvailableFreeSpace) Then

                        'Out of drive space... with 500MB buffer
                        Throw New InvalidOperationException("Not enough drive space.")

                    End If

                    Try

                        IO.File.WriteAllBytes(destFilePath, msg.Content)


                    Catch ex As Exception

                        Exceptions.LogException(ex)

                        Throw New FaultException(Of String)(ex.Message, "Error uploading " & msg.FileName & ". " & _
                            "Please check your site logs for details.")


                    Finally


                        Me.SyncFilesSystem(msg.Header.PortalID)


                    End Try


                Catch ex As Exception

                    Exceptions.LogException(ex)

                    Throw New FaultException(Of String) _
                        (ex.Message, CleanErrorMessage(msg.Header.PortalID, ex.Message))

                End Try



            Else

                Throw Me.GetLoggedNotAuthorizedEx(msg.Header)

            End If



        End Sub


#End Region

    End Class


End Namespace